define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";



	var base = require("../../custom/base/component-base.js");

	var async = require("../../custom/base/component-async.js");
	
	var validate = require("../../custom/base/component-validate.js");
	
		
	base.init(base);
	
	
	exports.init=function(m) {
		
		window.$ldh_register = m;
		
	
	};
	
	//同意注册协议
	exports.agree=function(obj){
		
		if($(obj).hasClass("checked")){
			
			$(obj).removeClass("checked");
			
			$("#cb_agree").val("0");
			
		}else{
			
			$(obj).addClass("checked");
			
			$("#cb_agree").val("1");
		}
				
	};
	
	//注册
	exports.register=function(){

		var mobile=$("#register_mobile").val();
		
		var validateCode=$("#register_validateCode").val();
		
		var password=$("#register_password").val();
		
		if(!validate.isMobile(mobile)){
			
			showTips("请填写正确的手机号码");
			
			return;
		}
		
		if(validate.isNull(validateCode)){
			
			showTips("请输入短信验证码");
			
			return;
		}

		if(!validate.isLoginPassword(password)){
			
			showTips("密码必须为6-20位数字，字母，符号组合");
			
			return;
		}
		
		if($("#cb_agree").val()!="1"){
			
			showTips("请勾选《云便利商店注册协议》");
			
			return;
		}
				
		var params={
				
			"mobile":mobile,
			
			"validateCode":validateCode,
			
			"password":MD5(password)
			
		};
		
		base.showLoading();
		
		eval(Wind.compile("async", function() {

			var data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Reg/register.do", params));

			if(data.code =="1"){
				//注册成功,自动登录
				
				showTips("注册成功,正在登录...");
				
				
				 // 加载一个模块
				  require.async([ctx + '/v1/h5/js/custom/login/module-login.js'], function(login) {
					  
					  login.autoLogin(mobile,password,base.getParameter("fromPage"));
					  
					  
				  });
				
				
			}else{
				
				showTips(data.message);
				
			}

			base.cancelLoading();
			
			
		}))().start();
		
	};
	

	//验证手机号是否注册过
	exports.validateMobile=function(inputId,btnId){
		
		var mobile = $(inputId).val();
		
		if(!validate.isMobile(mobile)){
			
			showTips("请填写正确的手机号码");
			
			return;
		}
	
		$(btnId).attr("disabled", "disabled");

		base.showLoading();
		
		if(btnId=="#btn_find_request_code"){
			
			sendMessage(mobile,btnId);
			
			return;
		}

		var params={
				"sign":MD5(mobile+"_sms_"),
				"mobile":mobile
			};
		
		eval(Wind.compile("async", function() {

			var data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Reg/validateMobile.do", params));
			
			if(data.code == "1"){
				//未注册,发送验证码
				sendMessage(mobile,btnId);
			}else{
				
				showTips(data.message);
				
				$(btnId).html("免费获取验证码");
				
				//如果该手机已注册,则可以马上重新验证
				$(btnId).attr("disabled", false);
			}
			
			base.cancelLoading();

		}))().start();
		
	};
	
	//发送短信验证码
	function sendMessage(mobile,btnId){
		
		var params={
				"sign":MD5(mobile+"_sms_"),
				"mobile":mobile
			};

		eval(Wind.compile("async", function() {

			var data;
			
			if(btnId=="#btn_find_request_code"){
				
				data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Login/sendValidateCode.do", params));

			}else{
				
				
				data = $await(async.DDF_AsyncRequest("POST",ctx+"/shopweb/Reg/sendValidateCode.do", params));

			}
			
			
			if(data.code == "1"){
				
				task(btnId).start();
				
			}else{
				
				//如果获取验证码失败,则可以马上重新获取
				$(btnId).attr("disabled", false);
				
				$(btnId).html("免费获取验证码");
				
				showTips(data.message);
			
			}

		}))().start();
	}
	
	//倒计时
	var wait = 90;
	
	var tips_task = eval(Wind.compile("async", function (btnId) {
		wait--;
	    $await(Wind.Async.sleep(1000));
	    if(wait==0){
	    	//倒计时完成,发送验证码按钮生效
	    	$(btnId).attr("disabled", false);
	    	$(btnId).html("免费获取验证码");
	    	
	    }else{
	    	
	    	$(btnId).html(wait + "秒后重新获取");
	    }
	    
	}));
	
	var task = eval(Wind.compile("async", function (btnId) {
	    $await(tips_task(btnId));
	    if(wait>0){
	      task(btnId).start();
	    }else{
	    	wait=90;
	    }
	}));
	
});